#!ratlperl
# --------

use lib "\\\\crdw0234\\CC_STORAGE\\SNAPSHOT_DATA\\adminCCRD_trigger_view\\CC_TOOLS\\config";

use Administration;
use cqtools;

# check the argument
# ------------------
if ($#ARGV<0){
   print"\nNo argument given!\n";
   usage();
}
if ($ARGV[0] eq "-h" || $ARGV[0] eq "-help"){
   usage();
}
if ($ARGV[0] !~ /^select/){
   print"\nNot a select statement!\n";
   usage();
}
$sql_statement = $ARGV[0];

# get the result for the select statement
# ---------------------------------------
my $cq  = cq_rdonly(CqObject->new('CRD:PF91'));
my $cqq = cq_prepare($cq, "$sql_statement");

die("ClearQuest ERROR: " . cq_error()) unless $cqq;

open (OUTPUTFILE, "> ssqa.txt") or die "Can't open file : $!";
foreach $row (cq_select($cq, $cqq)) {
#	print join('; ', @{$row}), "\n";
	
	$new_row = join('#property#', @{$row}), "\n";
	$new_row =~ s/\r\n/<br>/g;
	print OUTPUTFILE "$new_row\n";
	print "$new_row\n";
}
close OUTPUTFILE;
# usage
# -----
sub usage {
print "\n$0 \"<sql statement>\"\n".
     "Example : $0 \"select distinct T1.dbid,T1.id,T1.headline from cr T1,product T5 where T1.product = T5.dbid and (T1.dbid <> 0 and (T5.product = 'Diagnostic Tool'))\"\n\n";
exit (1);
}
